"use client";
import { UserInfoRep, UserVipInfo, Wallet, checkCodeApi } from "@/api/user";
import {
    BalanceContent,
    BonusContent,
    FreeContent,
    ReplayContent,
} from "@/components/ModalPopup/WalletDescribeModal";
import TipsModal, { ModalProps } from "@/components/TipsModal";
import { Link, useRouter } from "@/i18n/routing";
import { useWalletStore } from "@/stores/useWalletStore";
import { WalletEnum } from "@/types";
import { vipImages } from "@/utils/constant";
import { flatPoint, percentage } from "@/utils/methods";
import { ProgressBar, Toast, Badge } from "antd-mobile";
import { useTranslations } from "next-intl";
import Image from "next/image";
import { Fragment, useRef, useState } from "react";

type Props = {
    userInfo: UserInfoRep;
    userMoney: Wallet;
    userVip: UserVipInfo;
};
const VipCard = (props: { userVip: UserVipInfo }) => {
    const { userVip } = props;
    const t = useTranslations("ProfilePage");

    // Vip 图标
    const vipIconElement = vipImages.map((item, index) => {
        if (item.leve === userVip.vip_level) {
            return (
                <Fragment key={index}>
                    <Image src={item.src} alt={"vip"} height={110} width={100} />
                    <span className={"icon-level"} style={{ color: item.color }}>
                        {item.leve}
                    </span>
                </Fragment>
            );
        }
    });
    return (
        <div className={"vip-card"}>
            <div className={"vip-card__icon"}>{vipIconElement}</div>
            <div className={"vip-card-process"}>
                {/*<div className={"process-top"}>{userVip.vip_exp}xp</div>*/}
                <div>
                    <ProgressBar
                        percent={percentage(userVip.vip_exp, userVip.vip_score_exp)}
                        style={{
                            "--fill-color": "#fb8b05",
                            "--track-width": "0.0694rem",
                        }}
                    />
                </div>
                <div className={"process-bottom"}>
                    <span>VIP{userVip.vip_level}</span>
                    <span className={"process-bottom-desc"}>
                        {t("expTips", {
                            exp: flatPoint(userVip.vip_score_exp - userVip.vip_exp),
                        })}
                    </span>
                    <span>
                        VIP
                        {userVip.vip_next_level}
                    </span>
                </div>
            </div>
        </div>
    );
};

const WalletCard = (props: { userMoney: Wallet }) => {
    const { userMoney } = props;
    const t = useTranslations("ProfilePage");

    const tipsRef = useRef<ModalProps>(null);
    const [tipsStatus, setTipsStatus] = useState<keyof typeof WalletEnum>("Bonus");

    const modalHandler = (key: keyof typeof WalletEnum) => {
        setTipsStatus(key);
        tipsRef.current?.onOpen();
    };

    // 彩金、免费币、重玩币提现到钱包操作
    const handleAcquire = async () => {
        tipsRef.current?.onClose();
        // checkCodeApi({ code: "123", mobile: "18215519037" })
        // .then((res) => {
        //     if (res.code === 200) {
        //         Toast.show("领取成功!")
        //         setTimeout(() => {
        //             tipsRef.current?.onClose();
        //         }, 1000)
        //         return;
        //     }
        //     Toast.show("领取失败!");
        // })
        // .catch((error) => {
        //     Toast.show("领取失败!");
        // });
    };

    return (
        <>
            <TipsModal
                ref={tipsRef}
                title={
                    <div className={"flex items-center"}>
                        <i
                            className={"iconfont icon-liwuhuodong mr-[0.0694rem] text-[0.2778rem]"}
                        ></i>
                        {t("modalTitle")}
                    </div>
                }
            >
                {/*现金*/}
                {tipsStatus === WalletEnum.Balance ? <BalanceContent wallet={userMoney} /> : null}
                {/*  彩金*/}
                {tipsStatus === WalletEnum.Bonus ? <BonusContent wallet={userMoney} handleAcquire={handleAcquire}/> : null}
                {/* 免费币 */}
                {tipsStatus === WalletEnum.Free ? <FreeContent wallet={userMoney} handleAcquire={handleAcquire}/> : null}
                {/*  重玩币 */}
                {tipsStatus === WalletEnum.Replay ? <ReplayContent wallet={userMoney} handleAcquire={handleAcquire}/> : null}
            </TipsModal>
            <div className="coin">
                <div className={"coin_right_wallet"}>
                    <div
                        className={"wallet_left_border"}
                        onClick={() => modalHandler(WalletEnum.Balance)}
                    >
                        <span className="coin_left__icon iconfont icon-qianbao3"></span>
                        <section>
                            <div className={"wallet_header"}>
                                <span>{t("balance")}</span>
                                <Image
                                    className="wallet_header__icon"
                                    src="/img/a.png"
                                    alt="question"
                                    width={15}
                                    height={15}
                                />
                            </div>
                            <div className="num">
                                <span className="uppercase">brl </span>
                                <span>{userMoney.score || 0.0}</span>
                            </div>
                        </section>
                    </div>
                    <div
                        className={"wallet_right_content"}
                        onClick={() => modalHandler(WalletEnum.Bonus)}
                    >
                        <Badge content={percentage(userMoney.current_point_rollover, userMoney.target_point_rollover) < 100 ? null : Badge.dot} style={{right: "10px"}}>
                            <span className="coin_left__icon iconfont icon-qianbao3"></span>
                        </Badge>
                        <section>
                            <div className={"wallet_header"}>
                                {t("bonus")}
                                <Image
                                    className="wallet_header__icon"
                                    src="/img/a.png"
                                    alt="question"
                                    width={15}
                                    height={15}
                                />
                            </div>
                            <div className="num">
                                <span className="uppercase">brl </span>
                                <span>{userMoney.point || 0.0}</span>
                            </div>
                        </section>
                    </div>
                    <div
                        className={"wallet_left_border"}
                        onClick={() => modalHandler(WalletEnum.Free)}
                    >                   
                        <Badge content={percentage(userMoney.current_free_score_rollover, userMoney.target_free_score_rollover) < 100 ? null : Badge.dot} style={{right: "10px"}}>
                            <span className="coin_left__icon iconfont icon-qianbao3"></span>
                        </Badge>
                        <section>
                            <div className={"wallet_header"}>
                                {t("free")}
                                <Image
                                    className="wallet_header__icon"
                                    src="/img/a.png"
                                    alt="question"
                                    width={15}
                                    height={15}
                                />
                            </div>
                            <div className="num">
                                <span className="uppercase">brl </span>
                                <span>{userMoney.free_score || 0.0}</span>
                            </div>
                        </section>
                    </div>
                    <div
                        className={"wallet_right_content"}
                        onClick={() => modalHandler(WalletEnum.Replay)}
                    >
                        <Badge content={percentage(userMoney.current_lose_score_rollover, userMoney.target_lose_score_rollover) < 100 ? null : Badge.dot} style={{right: "10px"}}>
                            <span className="coin_left__icon iconfont icon-qianbao3"></span>
                        </Badge>
                        <section>
                            <div className={"wallet_header"}>
                                {t("replay")}
                                <Image
                                    className="wallet_header__icon"
                                    src="/img/a.png"
                                    alt="question"
                                    width={15}
                                    height={15}
                                />
                            </div>
                            <div className="num">
                                <span className="uppercase">brl </span>
                                <span>{userMoney.lose_score || 0.0}</span>
                            </div>
                        </section>
                    </div>
                </div>
            </div>
        </>
    );
};
export const ProfileHeader = (props: Props) => {
    const { userInfo, userVip } = props;
    const t = useTranslations("ProfilePage");

    const wallet = useWalletStore((state) => state.wallet);
    const router = useRouter();

    const handler = () => {
        if (!!wallet.score) {
            router.push("/withdraw");
        } else {
            Toast.show("no money ");
        }
    };
    return (
        <>
            <div className={"userContent"}>
                <div className={"userInfo"}>
                    <div>
                        <div className={"bgImg"}>
                            <Image
                                src={"/img/avatar.png"}
                                className={"h-[100%] w-[100%]"}
                                alt={"avatar"}
                                width={120}
                                height={120}
                            />
                        </div>
                        <div>
                            <span>{t("Conta")}</span>
                            <span className="phone">{userInfo?.user_phone || ""}</span>
                        </div>
                    </div>

                    <Link className="goto iconfont icon-xiangzuo1" href={`/changePassword`}></Link>
                </div>
                {/*vipcard*/}
                <VipCard userVip={userVip} />

                <WalletCard userMoney={wallet} />
            </div>

            <div className="link">
                <Link href={"/deposit"} className={"btn"}>
                    {t("Depósito")}
                </Link>

                <p className={"btn"} onClick={handler}>
                    {t("Sacar")}
                </p>
            </div>
        </>
    );
};
